package business_service;


import i_business.I_User;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import dao_pool.Pool;
import dao_service.UserImp;

import Protocol.admin.AdminBehave_Q;
import Protocol.admin.AdminBehave_R;
import Protocol.app.VisitFriendSpace_Q;
import Protocol.app.VisitFriendSpace_R;
import Protocol.contact.Contact;
import Protocol.contact.ContactGroup;
import Protocol.contact.Contact_Q;
import Protocol.contact.Contact_R;
import Protocol.contact.CreateGroup_Q;
import Protocol.contact.CreateGroup_R;
import Protocol.contact.DeleteGroup_Q;
import Protocol.contact.DeleteGroup_R;
import Protocol.contact.UpdateGroup_Q;
import Protocol.contact.UpdateGroup_R;
import Protocol.user.BasePersonInfo;
import Protocol.user.BasePersonInfo_Q;
import Protocol.user.BasePersonInfo_R;
import Protocol.user.DetailPersonInfo;
import Protocol.user.DetailPersonInfo_Q;
import Protocol.user.DetailPersonInfo_R;
import Protocol.user.Login_Q;
import Protocol.user.Login_R;

public class UserService implements I_User{
	Pool        m_Pool = Pool.getInstance();           

	
	public BasePersonInfo_Q doBasePersonInfo(BasePersonInfo_R bpr) {
		
	return null;
	}

	@Override
	public DetailPersonInfo_Q doDetailPersonInfo(DetailPersonInfo_R dpr) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public AdminBehave_Q doAdmin(AdminBehave_R abr) {
		// TODO Auto-generated method stub
		Connection conn=m_Pool.getConnection();	
		Statement statement = null;
		AdminBehave_Q abq=null;
		 SimpleDateFormat sFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			 UserImp ui=new UserImp();
			statement=conn.createStatement();
		  if(abr.getUserList().size()!=0){
			  if(abr.getOperationType().equals("add")){
				  for(int i=0;i<abr.getUserList().size();i++){
					  String sql="SELECT user_number FROM user";
					 
					  ResultSet rs=statement.executeQuery(sql);
					 
					  while(rs.next()){
						  if(abr.getUserList().get(i).getUser_number().equals(rs.getString("user_number"))){
							  
						  }
						  else{
							  ui.insertUser(abr.getUserList().get(i));
							  abq.setStatus("success");
							  abq.getLog().setAdmin_id(abr.getAdmin_id());
							  Date d=new Date();
							  abq.getLog().setDatetime(new java.sql.Date(d.getTime()));
							  abq.getLog().setOperationType("add");
							  abq.getLog().setStatus("success");
							  abq.getLog().setDesc("succeed to add one message");
							  String n_sql="Insert into adminlog (operator_id,operation_type,operation_time,operation_description,operation_status) values (abq.getLog().getAdmin_id(),abq.getLog().getOperationType(),abq.getLog().getDatetime(),abq.getLog().getDesc(),abq.getLog().getStatus())";
							  statement.executeQuery(n_sql);
						  }
					  }
				 
				 
				  }
			  }else if(abr.getOperationType().equals("delete")){
				  
			  }else if(abr.getOperationType().equals("update")){
				  
			  }
		  }else if(abr.getThreadLogStatusList().size()!=0){
			  
		  }
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			m_Pool.freeConnection(conn);
		}
		return abq;
		
		
	
	}
	
}
